package com.ailanyin.admin.service.impl;

import com.ailanyin.admin.service.SysLogService;
import com.ailanyin.admin.service.SysUserService;
import com.ailanyin.common.exception.Asserts;
import com.ailanyin.mapper.SysLogMapper;
import com.ailanyin.model.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/28 0028 下午 14:46
 */
@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysUserService sysUserService;
    @Resource
    private SysLogMapper logMapper;

    /**
     * 新增日志
     *
     * @param sysLog log
     */
    @Override
    public void insertLog(SysLog sysLog) {
        logMapper.insertLog(sysLog);
    }

    /**
     * 分页获取日志列表
     *
     * @param sysLog sysLog
     * @return list
     */
    @Override
    public List<SysLog> list(SysLog sysLog) {
        return logMapper.selectByParam(sysLog);
    }

    @Override
    public void cleanLog() {
        checkWhetherAllow();
        logMapper.deleteBatchIds(null);
    }

    /**
     * 批量删除日志
     *
     * @param logIds ids
     */
    @Override
    public void removeLog(List<String> logIds) {
        checkWhetherAllow();
        logMapper.deleteBatchIds(logIds);
    }

    private void checkWhetherAllow() {
        if (!sysUserService.getCurrentUser().isAdmin()) {
            Asserts.fail("非超管禁止操作！");
        }
    }
}
